#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2024/9/20 17:46
# @Author  : Tom_zc
# @FileName: gitcode_helper.py
# @Software: PyCharm
import logging
import os

from .request_handler import RequestHandler


class GitCodeHelper:
    get_pr_url = "{domain}/api/v5/repos/{owner}/{repo}/pulls/{number}?access_token={token}"
    create_label_url = "{domain}/api/v5/repos/{owner}/{repo}/pulls/{number}/labels?access_token={token}"
    delete_label_url = "{domain}/api/v5/repos/{owner}/{repo}/pulls/{number}/labels/{name}?access_token={token}"
    create_comment_url = "{domain}/api/v5/repos/{owner}/{repo}/pulls/{number}/comments?access_token={token}"
    create_webhook_url = "{domain}/api/v5/repos/{owner}/{repo}/hooks?access_token={token}"
    list_webhook_url = "{domain}/api/v5/repos/{owner}/{repo}/hooks?access_token={token}"
    delete_webhook_url = "{domain}/api/v5/repos/{owner}/{repo}/hooks/{id}?access_token={token}"
    get_pr_files_url = "{domain}/api/v5/repos/{owner}/{repo}/pulls/{number}/files?access_token={token}"
    get_ci_config_url = "{domain}/api/v5/repos/{owner}/{repo}/contents/{path}?access_token={token}"

    def __init__(self, owner, repo, number, token,
                 domain=os.getenv("gitcode_api_domain"),
                 request_handler=RequestHandler()):
        self.request_handler = request_handler
        self.owner = owner
        self.repo = repo
        self.number = number
        self.token = token
        self.domain = domain

    def get_pr(self):
        url = self.get_pr_url.format(domain=self.domain, owner=self.owner,
                                     repo=self.repo, number=self.number, token=self.token)
        return self.request_handler.get(url)

    def create_label(self, json_data):
        url = self.create_label_url.format(domain=self.domain, owner=self.owner,
                                           repo=self.repo, number=self.number, token=self.token)
        _, resp = self.request_handler.post(url, json_data)
        return resp

    def delete_label(self, label_name):
        url = self.delete_label_url.format(domain=self.domain, owner=self.owner,
                                           repo=self.repo, number=self.number, name=label_name, token=self.token)
        return self.request_handler.delete(url)

    def create_comment(self, data):
        url = self.create_comment_url.format(domain=self.domain, owner=self.owner,
                                             repo=self.repo, number=self.number, token=self.token)
        _, resp = self.request_handler.post(url, data)
        return resp

    def create_webhook(self, data):
        """
        :param data: {
            "url": "https://baidu.com",
            "push_events": True,
            "tag_push_events": False,
            "issues_events": False,
            "note_events": True,
            "merge_requests_events": True
        }
        :return:
        """
        url = self.create_webhook_url.format(domain=self.domain, owner=self.owner,
                                             repo=self.repo, token=self.token)
        logging.info("create webhook: {}/{}/{}/{}".format(self.domain,self.owner, self.repo, url, data))
        _, resp = self.request_handler.post(url, data)
        return resp

    def get_webhooks(self):
        url = self.list_webhook_url.format(domain=self.domain, owner=self.owner,
                                           repo=self.repo, token=self.token)
        return self.request_handler.get(url)

    def delete_webhook(self, webhook_id):
        url = self.delete_webhook_url.format(domain=self.domain, owner=self.owner, id=webhook_id,
                                             repo=self.repo, token=self.token)
        return self.request_handler.delete(url)

    def get_pr_files(self):
        url = self.get_pr_files_url.format(domain=self.domain, owner=self.owner,
                                           repo=self.repo, number=self.number, token=self.token)
        return self.request_handler.get(url)

    def get_contents(self, path):
        url = self.get_ci_config_url.format(domain=self.domain, owner=self.owner,
                                            repo=self.repo, path=path, token=self.token)
        return self.request_handler.get(url)
